home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3967 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  67 lines

  1. Path: seas.smu.edu!not-for-mail
  2. From: dbowman@post.smu.edu (Damon Bowman)
  3. Newsgroups: comp.lang.c++
  4. Subject: precision methods
  5. Date: 26 Jan 1996 18:01:11 -0600
  6. Organization: Southern Methodist University
  7. Sender: usenet@seas.smu.edu
  8. Message-ID: <4ebq07$54r@sun.cis.smu.edu>
  9. Reply-To: dbowman@post.smu.edu
  10. NNTP-Posting-Host: sun.cis.smu.edu
  11. X-Nntp-Posting-Host: ax4-22.ppp.smu.edu
  12. X-Newsreader: Forte Free Agent 1.0.82
  13.  
  14.  
  15. I get a strange compiler warning when I try to compile this code.
  16. It's from a C++ textbook by Deitel/Deitel.
  17.  
  18. The compiler warning follows the code.  What is the source of this
  19. warning message?  
  20.  
  21. I know compiler warnings are off-topic for this newsgroup, but I also
  22. have a C++ language question:  
  23.  
  24. How is the precision member functions supposed to work?  The book says
  25. that it controls the number of places to the RIGHT of the decimal.  In
  26. other words, 
  27.  
  28. float a = 45.67;
  29. cout.precision(2);
  30. cout << a;
  31.  
  32. Should output 45.67.  It actually outputs 46 on my machine.  Is the
  33. book wrong (wouldn't be the first time I've found blatant errors in
  34. programming books).
  35.  
  36. Here's the sample code in the text, followed by the compiler warning:
  37.  
  38. //fig11_17.cpp
  39. //Controlling precision of floating-point values
  40. #include <iostream.h>
  41. #include <iomanip.h>
  42. #include <math.h>
  43.  
  44. main()
  45. {
  46. double root2 = sqrt(2.0);
  47. cout << "Square root of 2 with precisions 0-9." << endl
  48. << "Precision set by the precision member function:" << endl;
  49.  
  50. for (int places = 0; places <= 9; places++)
  51.     {
  52.     cout.precision(places);
  53.     cout << root2 << endl;
  54.     }
  55. cout << endl << "Precision set by the setprecision manipulator:" <<
  56. endl;
  57.  
  58. for (places = 0; places <=9; places++)
  59.     cout << setprecision(places) << root2 << endl;
  60. return 0;
  61. }        
  62.  
  63. d:\msvc\test\fig11_17.cpp(21) : warning C4270: 'initializing' : do not
  64. initialize a non-const 'class ::__SMANIP_int __near &' with a
  65. non-lvalue 'class ::__SMANIP_int ' function return
  66.  
  67.